<?php

namespace app\api\model;

use think\Db;
use think\Model;

class Shuttle extends Model
{
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    public function before()
    {
        return $this->hasOne(Users::class, 'id', 'before_user_id');
    }

    public function after()
    {
        return $this->hasOne(Users::class, 'id', 'after_user_id');
    }

    public function getBeforeClassAttr($value)
    {
        return $value ? date("H:i:s", $value) : '';
    }

    public function getAfterClassAttr($value)
    {
        return $value ? date("H:i:s", $value) : '';
    }

    public function getCreateTimeAttr($value)
    {
        return $value ? date("Y-m-d", $value) : '';
    }


    public static function add($student_id, $teacher_id, $user_id)
    {
        return self::create([
            'before_user_id' => $user_id,
            'student_id' => $student_id,
            'teacher_id' => $teacher_id,
            'before_class' => time()
        ]);
    }

    /**
     * 今天接受记录
     */
    public static function getByToday($studentIds, $teacher_id, $user_id)
    {
        Db::startTrans();
        try {
            foreach ($studentIds as $student_id) {
                $student = self::where('student_id', $student_id)->whereTime('createtime', 'today')->find();
                if (!$student) {
                    Shuttle::add($student_id, $teacher_id, $user_id);
                } else {
                    $student->after_user_id = $user_id;
                    $student->after_class = time();
                    $student->count += 1;
                    $student->save();
                }
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        return isset($e) ? false : true;
    }

    /**
     * 月记录
     */
    public static function getByDate($date, $student_id)
    {
        $start_time = strtotime($date);
        $end_time = strtotime("+1 month -1 seconds", $start_time);
        return self::with(['before', 'after'])->where('student_id', $student_id)->whereTime('createtime', 'between', [$start_time, $end_time])->select();
    }

    public static function getRecords($date, $student_id)
    {
        $records = self::getByDate($date, $student_id);
        if (!$records) {
            return [];
        }
        $before_name = '';
        $before_relation = '';
        $after_name = '';
        $after_relation = '';
        foreach ($records as $val) {
            if ($val->before) {
                $before = self::getUser($val->before->phone);
                $before_name = $before['username'];
                $before_relation = $before['relation'];
            }
            if ($val->after) {
                $after = self::getUser($val->after->phone);
                $after_name = $after['username'];
                $after_relation = $after['relation'];
            }
            $val['before_username'] = $before_name;
            $val['before_relation'] = $before_relation;
            $val['after_username'] = $after_name;
            $val['after_relation'] = $after_relation;
            unset($val['before']);
            unset($val['after']);
        }
        return $records;
    }

    private static function getUser($phone)
    {
        $username = '';
        $relation = '';
        $parent = Parents::getByPhone($phone);
        if ($parent) {
            $username = $parent->username;
        } else {
            $friend = Friends::with('relations')->where('phone', $phone)->find();
            if ($friend) {
                $username = $friend->username;
                $relation = $friend->relations ? $friend->relations->name : '';
            }
        }
        return [
            'username' => $username,
            'relation' => $relation,
        ];
    }
}
